﻿using DongFangGuoKai.TaskPlatform.Commn.Attributes;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace DongFangGuoKai.TaskPlatform.Commn.LogModule
{
    public class LogHelper<T>
    {
        private readonly ILogger<T> _logger;

        public LogHelper(ILogger<T> logger)
        {
            _logger = logger;
        }

        /// <summary>
        /// Error
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="msg"></param>
        /// <param name="code"></param>
        public void LogError(string? url, string? data, string? msg, string? code = "RemoteError")
        {
            Type type = typeof(T);
            string key = string.Empty;
            string group = string.Empty;
            if (type.IsDefined(typeof(JobAttribute), true))
            {
                JobAttribute attribute = type.GetCustomAttribute<JobAttribute>();
                key = attribute.JobKey;
                group = attribute.JobGroup;
            }
            string errMsg = $"【\n[任务Key：{key}] \n[任务Group：{group}]  \n [请求Url：{url}]  \n[请求数据:{data}] \n[状态码：{code}]   \n [描述信息:{msg}]  \n[执行时间：{DateTime.Now}]\n 】";
            _logger.LogError(errMsg);
        }
        public void LogError(string? url, string? data, string? msg)
        {
            LogError(url, data, msg, "500");
        }
        public void LogError(string? url, string? data)
        {
            LogError(url, data, null);
        }
        public void LogError(string? url)
        {
            LogError(url, null);
        }
        public void LogCtmError(string msg)
        {
            Type type = typeof(T);
            string key = string.Empty;
            string group = string.Empty;
            if (type.IsDefined(typeof(JobAttribute), true))
            {
                JobAttribute attribute = type.GetCustomAttribute<JobAttribute>();
                key = "[任务Key：" + attribute.JobKey + "]";
                group = "[任务Group：" + attribute.JobGroup + "]";
            }

            _logger.LogError("【\n" + key + "\n" + group + "\n" + msg + $"[执行时间：{DateTime.Now}]" + "\n】");
        }

        /// <summary>
        /// Success
        /// </summary>
        /// <param name="url"></param>
        /// <param name="data"></param>
        public void LogSuc(string url, string data)
        {
            Type type = typeof(T);
            string key = string.Empty;
            string group = string.Empty;
            if (type.IsDefined(typeof(JobAttribute), true))
            {
                JobAttribute attribute = type.GetCustomAttribute<JobAttribute>();
                key = attribute.JobKey;
                group = attribute.JobGroup;
            }
            string sucMsg = $"【\n[任务Key：{key}]  \n[任务Group：{group}] \n[请求Url：{url}]  \n[请求数据:{data}]  \n[状态码：Success]   \n[执行时间：{DateTime.Now}] \n 】";
            _logger.LogInformation(sucMsg);
        }
        public void LogSuc(string url)
        {
            LogSuc(url);
        }
    }
}
